package com.dev.security.manager.service.impl;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specifications;
import org.springframework.stereotype.Service;

import com.dev.security.manager.beans.App;
import com.dev.security.manager.dao.AppRepository;
import com.dev.security.manager.dao.RoleAppUnitRepository;
import com.dev.security.manager.service.IAppService;
import com.dev.security.manager.service.IPermissionService;
import com.dev.security.util.Criteria;

/**
 * @author 潜行的青衣
 * @see
 */
@Service
public class AppService implements IAppService {
	
	@Autowired
	private AppRepository appRepository;
	
	@Autowired
	private RoleAppUnitRepository roleAppUnitRepository;
	
	@Autowired
	private IPermissionService permissionService;

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IAppService#findPage(com.dev.security.manager.beans.App, int, int, org.springframework.data.domain.Sort)
	 */
	@Override
	public Page<App> findPage(final App app, int page, int size, Sort sort) {
		Criteria<App> criteria = new Criteria<App>();
		if(StringUtils.isNotEmpty(app.getName())) {
			criteria.and().like("name", new StringBuilder()
				.append("%").append(app.getName()).append("%").toString());
		}
		if(StringUtils.isNotEmpty(app.getUri())) {
			criteria.and().like("uri", new StringBuilder()
				.append("%").append(app.getUri()).append("%").toString());
		}
		if(StringUtils.isNotEmpty(app.getIcon())) {
			criteria.and().like("icon", new StringBuilder()
				.append("%").append(app.getIcon()).append("%").toString());
		}
		Specifications<App> spec = criteria.toSpecifications();
		Pageable pageable = new PageRequest(page - 1, size, sort);
		if(spec != null) {
			return this.appRepository.findAll(spec, pageable);
		} else {
			return this.appRepository.findAll(pageable);
		}
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IAppService#save(java.util.List)
	 */
	@Override
	public void save(List<App> apps) {
		this.appRepository.save(apps);
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IAppService#findApps(java.lang.String[])
	 */
	@Override
	public List<App> findApps(List<String> role_ids) {
		List<App> result = roleAppUnitRepository.findAllApps(role_ids);
		Iterator<App> it = result.iterator();
		App app = null;
		while(it.hasNext()) {
			app = it.next();
			app.setPerms(this.permissionService.findPermissionMap(role_ids, app.getId()));
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IAppService#findAllApps()
	 */
	@Override
	public List<App> findAllApps() {
		List<App> result = this.appRepository.findAll(new Sort(new Order(Direction.ASC, "index")));
		Iterator<App> it = result.iterator();
		App app = null;
		while(it.hasNext()) {
			app = it.next();
			app.setPerms(this.permissionService.findPermissionMap(app.getId()));
		}
		return result;
	}

}
